package sonic.game.item;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import sonic.game.Game;
import sonic.game.animation.Animation;
import sonic.game.character.Sonic;
import sonic.game.tile.Tile;
import sonic.game.util.Vector2D;

/**
 * Dropped Ring
 * 
 * A ring dropped by Sonic when he's hurt
 * 
 * @author Richard Greenlees
 *
 */

public class GameItemDroppedRing extends GameItem {

	protected Animation idleAnim;
	protected Animation currentAnim;
	private boolean bFlashing = false;
	private float flashTime = 0.1f;
	private float flashTimer = 0.0f;
	
	public GameItemDroppedRing(Vector2D startPos)
	{
		super(startPos);
		collisionType = eCollisionType.CT_TOUCHALL;
		physicsType = ePhysicsType.PHYS_FALLING;
		
		try
		{
			BufferedImage idleAnim1;
			BufferedImage idleAnim2;
			BufferedImage idleAnim3;
			idleAnim1 = ImageIO.read(new File("Items/Ring_idle_1.png"));
			idleAnim2 = ImageIO.read(new File("Items/Ring_idle_2.png"));
			idleAnim3 = ImageIO.read(new File("Items/Ring_idle_3.png"));
			
			idleAnim = new Animation(0.5f, idleAnim1, idleAnim2, idleAnim3);
			currentAnim = idleAnim;
			collisionBoxX = 16.0f;
			collisionBoxY = 16.0f;
			bAlwaysRelevant = true;
		}
		catch (IOException e)
		{
			e.printStackTrace();
			destroy();
		}
	
		velocity.x = (float) (-220.0f*Math.random()*((Math.random() > 0.5) ? 1.0f : -1.0f));
		velocity.y = (float) (-220.0f*Math.random());
		
		lifeSpan = 5.0f;
		
		updateBounds();
	}
	
	public void paint(Graphics g)
	{
		if (currentAnim != null && !bFlashing)
		{
			g.drawImage(currentAnim.getCurrentFrame(), (int) location.x + (320-(int) Game.camera.location.x), (int) location.y + (240-(int) Game.camera.location.y), null);
		}
	}
	
	/** Bounce off walls when it hits them */
	public void hitWall(Tile hit)
	{
		velocity.x = -velocity.x*0.75f;
	}
	
	/** Bounce off surfaces */
	public void landedOn(Tile landed)
	{
		Vector2D newLoc = new Vector2D();
		newLoc.x = location.x;
		newLoc.y = landed.location.y-collisionBoxY;
		setLocation(newLoc);
		velocity.y = -velocity.y*0.75f;
	}
	
	/** Bounce off the underside of solid objects */
	public void landedOnBy(Tile landedBy)
	{
		velocity.y = -velocity.y*0.75f;
	}
	
	public void update(double deltaTime)
	{
		super.update(deltaTime);
		flashTimer += deltaTime;
		if (flashTimer > flashTime)
		{
			flashTimer = 0.0f;
			bFlashing = !bFlashing;
		}
		if (currentAnim != null)
		{
			currentAnim.update(deltaTime);
		}
	}
	
	/** Ignore touches by anything except a Sonic object */
	public void touch(Tile touched)
	{
		if (touched instanceof Sonic)
		{
			((Sonic) touched).rings++;
			createEffect(location.x, location.y, new Animation(0.25f, false, "Effects/Ring_collect_1.png", "Effects/Ring_collect_2.png", "Effects/Ring_collect_3.png"));
			playSound("sounds/Ring_collect.wav");
			destroy();
		}
	}
	
}
